//
//  WBPictureView.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 29/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

private let baseTag = 6666

class WBPictureView: UIView {
    var statusViewModel: WBStatusViewModel? {
        didSet {
            //先把所有的imageView隐藏(把所有的蓝子收起来)
            for view in self.subviews {
                view.isHidden = true
            }
            
            // 如果有图片
            if let pic_urls = statusViewModel?.pic_urls, pic_urls.count > 0 {
                //遍历pictureModels, 将pictureModel的图片展示出来(遍历萝卜, 找到蓝子, 把萝卜装进蓝子, 取出蓝子显示)
                for item in pic_urls.enumerated() {
                    let index = item.offset
                    let pictureModel = item.element
                    //给imageView赋值
                    var imageViewIndex = index
                    if pic_urls.count == 4, index > 1 {
                        imageViewIndex += 1
                    }
                    
                    let imageView = self.viewWithTag(imageViewIndex+baseTag) as! UIImageView
                    
                    imageView.wb_setImage(withURLString: pictureModel.thumbnail_pic!, placeHolder: #imageLiteral(resourceName: "avatar_default_big"))
                    //imageView有值,则显示
                    imageView.isHidden = false
                    
                    if index == 0 && pic_urls.count == 1 {
                        //小坑: 在自动布局的时候, UI的布局发生变化, 其frame或bounds的值不一定变化
                        //在swift中frame,size, point的元素的值,调整之后,马上生效
                    } else if index == 0 {
                        // imageView的宽
                        let imageWidth = (screenWidth - 4*12)/3
                        // 第一个imageview的rect
                        let rect = CGRect(x: 0, y: 0, width: imageWidth, height: imageWidth)
                        imageView.frame = rect
                    }
                }
            }
        }
        
    }
    
    /// 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 设置UI
extension WBPictureView {
    func setupUI () {
        
        backgroundColor = UIColor.softColor(withHue: .greenHue)
        
        // imageView的宽
        let imageWidth = (screenWidth - 4*12)/3
        // 第一个imageview的rect
        let rect = CGRect(x: 0, y: 0, width: imageWidth, height: imageWidth)
        // 添加9个子视图
        for i in 0..<9 {
            //行数
            let row = i / 3
            //列数
            let col = i % 3
            //算出每个imageView的rect
            let imageRect = rect.offsetBy(dx: (imageWidth+12)*CGFloat(col), dy: (imageWidth+12)*CGFloat(row))
            
            //创建并添加图片视图
            let imageview = UIImageView(frame: imageRect)
            //.scaleAspectFit: 等比例压缩
            //scaleToFill: 默认效果, 拉伸或缩小以适应容器
            //scaleAspectFill: 等比例显示可能会超出容器
            imageview.contentMode = .scaleAspectFill
            imageview.clipsToBounds = true
            imageview.tag = baseTag + i
            
            //添加点击事件
//            imageview.isUserInteractionEnabled = true
//            let tap = UITapGestureRecognizer(target: self, action: #selector(tap(tap:)))
//            imageview.addGestureRecognizer(tap)
//            
            imageview.backgroundColor = UIColor.softColor(withHue: .redHue)
            addSubview(imageview)
        }
        
        //如果超出视图的部分,则裁切掉
        clipsToBounds = true
    }
}

extension WBPictureView {
    @objc fileprivate func tap(tap: UITapGestureRecognizer) {
        var index = (tap.view as! UIImageView).tag - baseTag
        if let pic_urls = statusViewModel?.pic_urls, pic_urls.count == 4, index > 2 {
            index -= 1
        }
        print(index)
    }
}
